/*
    Copyright (C) 2008-2009 Dmitry Gusev <dmitry.gusev@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

package dmitrygusev.web4konverta.mobile;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStoreException;
import net.dclausen.microfloat.MicroDouble;

/**
 * @author dmitrygusev
 */
public class EnvelopeMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    
    private Controller c = Controller.getInstance();
    private ApplicationState as = ApplicationState.getInstance();

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private java.util.Hashtable __previousDisplayables = new java.util.Hashtable();
    private Command cmdAbout;
    private Command cmdShowExpences;
    private Command cmdBack;
    private Command cmdProcessLogin;
    private Command cmdExitTypeExit;
    private Command cmdRefreshEnvelope;
    private Command cmdShowEnvelopeDetails;
    private Command okCommand;
    private Command cmdSave;
    private Command cmdCancel;
    private Command cmdExit;
    private Form editAccountInfo;
    private StringItem connectionStatus;
    private TextField accountName;
    private TextField accountPassword;
    private Form about;
    private StringItem aboutText;
    private ImageItem logoAbout;
    private Form selectEnvelopeDate;
    private StringItem status;
    private DateField envelopeDate;
    private ChoiceGroup personChoise;
    private TextBox editExpenses;
    private Form envelopeDetails;
    private StringItem weeklyBudget;
    private StringItem spentLeftForTheWeek;
    private StringItem dailyBudget;
    private StringItem spentLeftForADay;
    private Alert loginAlert;
    private Image logo;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The MIDlet constructor.
     */
    public EnvelopeMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    /**
     * Switches a display to previous displayable of the current displayable.
     * The <code>display</code> instance is obtain from the <code>getDisplay</code> method.
     */
    private void switchToPreviousDisplayable() {
        Displayable __currentDisplayable = getDisplay().getCurrent();
        if (__currentDisplayable != null) {
            Displayable __nextDisplayable = (Displayable) __previousDisplayables.get(__currentDisplayable);
            if (__nextDisplayable != null) {
                switchDisplayable(null, __nextDisplayable);
            }
        }
    }
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        getAccountName().setString(as.getAppConfig().getAccountName());
        getAccountPassword().setString(as.getAppConfig().getAccountPassword());
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        isAccountInfoAvailable();//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        Displayable __currentDisplayable = display.getCurrent();
        if (__currentDisplayable != null  &&  nextDisplayable != null) {
            __previousDisplayables.put(nextDisplayable, __currentDisplayable);
        }
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == about) {//GEN-BEGIN:|7-commandAction|1|169-preAction
            if (command == cmdBack) {//GEN-END:|7-commandAction|1|169-preAction
                // write pre-action user code here
                switchToPreviousDisplayable();//GEN-LINE:|7-commandAction|2|169-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|160-preAction
        } else if (displayable == editAccountInfo) {
            if (command == cmdAbout) {//GEN-END:|7-commandAction|3|160-preAction
                // write pre-action user code here
                switchDisplayable(null, getAbout());//GEN-LINE:|7-commandAction|4|160-postAction
                // write post-action user code here
            } else if (command == cmdExitTypeExit) {//GEN-LINE:|7-commandAction|5|177-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|177-postAction
                // write post-action user code here
            } else if (command == cmdProcessLogin) {//GEN-LINE:|7-commandAction|7|52-preAction
                // write pre-action user code here
                isAccountInfoAvailable();//GEN-LINE:|7-commandAction|8|52-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|164-preAction
        } else if (displayable == editExpenses) {
            if (command == cmdAbout) {//GEN-END:|7-commandAction|9|164-preAction
                // write pre-action user code here
                switchDisplayable(null, getAbout());//GEN-LINE:|7-commandAction|10|164-postAction
                // write post-action user code here
            } else if (command == cmdCancel) {//GEN-LINE:|7-commandAction|11|80-preAction
                switchDisplayable(null, getSelectEnvelopeDate());//GEN-LINE:|7-commandAction|12|80-postAction
                // write post-action user code here
            } else if (command == cmdSave) {//GEN-LINE:|7-commandAction|13|71-preAction
                getEditExpenses().setTitle("Запись затрат...");
                
                try {
                    processSaveExpenses();
                } catch (IOException e) {
                    getStatus().setText("Ошибка 4k-m-010: " + e.getClass().getName() + ": " + e.getMessage());
                }
                switchDisplayable(null, getSelectEnvelopeDate());//GEN-LINE:|7-commandAction|14|71-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|189-preAction
        } else if (displayable == envelopeDetails) {
            if (command == cmdBack) {//GEN-END:|7-commandAction|15|189-preAction
                // write pre-action user code here
                switchToPreviousDisplayable();//GEN-LINE:|7-commandAction|16|189-postAction
                // write post-action user code here
            } else if (command == cmdRefreshEnvelope) {//GEN-LINE:|7-commandAction|17|191-preAction
                getEnvelopeDetails().setTitle("Загрузка конверта...");

                processGrabEnvelope();

                updateEnvelopeDetails();
                
                switchDisplayable(null, getEnvelopeDetails());//GEN-LINE:|7-commandAction|18|191-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|19|205-preAction
        } else if (displayable == loginAlert) {
            if (command == okCommand) {//GEN-END:|7-commandAction|19|205-preAction
                // write pre-action user code here
                switchDisplayable(null, getEditAccountInfo());//GEN-LINE:|7-commandAction|20|205-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|163-preAction
        } else if (displayable == selectEnvelopeDate) {
            if (command == cmdAbout) {//GEN-END:|7-commandAction|21|163-preAction
                // write pre-action user code here
                switchDisplayable(null, getAbout());//GEN-LINE:|7-commandAction|22|163-postAction
                // write post-action user code here
            } else if (command == cmdExit) {//GEN-LINE:|7-commandAction|23|92-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|24|92-postAction
                // write post-action user code here
            } else if (command == cmdShowEnvelopeDetails) {//GEN-LINE:|7-commandAction|25|182-preAction
                updateEnvelopeDetails();
                switchDisplayable(null, getEnvelopeDetails());//GEN-LINE:|7-commandAction|26|182-postAction
                // write post-action user code here
            } else if (command == cmdShowExpences) {//GEN-LINE:|7-commandAction|27|35-preAction
                try {
                    prepareEditExpenses();
                    switchDisplayable(null, getEditExpenses());//GEN-LINE:|7-commandAction|28|35-postAction
                } catch (LoginException e) {
                    getStatus().setText(e.getMessage());
                } catch (Exception e) {
                    getStatus().setText("Ошибка 4k-m-501: " + e.getClass().getName() + ": " + e.getMessage());
                }
            }//GEN-BEGIN:|7-commandAction|29|7-postCommandAction
        }//GEN-END:|7-commandAction|29|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|30|
    //</editor-fold>//GEN-END:|7-commandAction|30|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: editAccountInfo ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of editAccountInfo component.
     * @return the initialized component instance
     */
    public Form getEditAccountInfo() {
        if (editAccountInfo == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            editAccountInfo = new Form("4 \u041A\u043E\u043D\u0432\u0435\u0440\u0442\u0430 - \u0412\u0445\u043E\u0434", new Item[] { getAccountName(), getAccountPassword(), getConnectionStatus() });//GEN-BEGIN:|14-getter|1|14-postInit
            editAccountInfo.addCommand(getCmdProcessLogin());
            editAccountInfo.addCommand(getCmdAbout());
            editAccountInfo.addCommand(getCmdExitTypeExit());
            editAccountInfo.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return editAccountInfo;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: selectEnvelopeDate ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of selectEnvelopeDate component.
     * @return the initialized component instance
     */
    public Form getSelectEnvelopeDate() {
        if (selectEnvelopeDate == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            selectEnvelopeDate = new Form("4 \u041A\u043E\u043D\u0432\u0435\u0440\u0442\u0430 - \u041A\u043E\u043D\u0432\u0435\u0440\u0442", new Item[] { getEnvelopeDate(), getPersonChoise(), getStatus() });//GEN-BEGIN:|27-getter|1|27-postInit
            selectEnvelopeDate.addCommand(getCmdShowEnvelopeDetails());
            selectEnvelopeDate.addCommand(getCmdShowExpences());
            selectEnvelopeDate.addCommand(getCmdAbout());
            selectEnvelopeDate.addCommand(getCmdExit());
            selectEnvelopeDate.setCommandListener(this);//GEN-END:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return selectEnvelopeDate;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: editExpenses ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of editExpenses component.
     * @return the initialized component instance
     */
    public TextBox getEditExpenses() {
        if (editExpenses == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            editExpenses = new TextBox("\u0414\u0438\u043C\u0430 - 2008-7-10", null, 100, TextField.ANY);//GEN-BEGIN:|28-getter|1|28-postInit
            editExpenses.addCommand(getCmdSave());
            editExpenses.addCommand(getCmdCancel());
            editExpenses.addCommand(getCmdAbout());
            editExpenses.setCommandListener(this);//GEN-END:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return editExpenses;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdShowExpences ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of cmdShowExpences component.
     * @return the initialized component instance
     */
    public Command getCmdShowExpences() {
        if (cmdShowExpences == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            cmdShowExpences = new Command("\u041F\u043E\u043A\u0430\u0437\u0430\u0442\u044C", Command.OK, 0);//GEN-LINE:|34-getter|1|34-postInit
            // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return cmdShowExpences;
    }
    //</editor-fold>//GEN-END:|34-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: envelopeDate ">//GEN-BEGIN:|42-getter|0|42-preInit
    /**
     * Returns an initiliazed instance of envelopeDate component.
     * @return the initialized component instance
     */
    public DateField getEnvelopeDate() {
        if (envelopeDate == null) {//GEN-END:|42-getter|0|42-preInit
            // write pre-init user code here
            envelopeDate = new DateField("\u0414\u0430\u0442\u0430 \u043A\u043E\u043D\u0432\u0435\u0440\u0442\u0430", DateField.DATE);//GEN-BEGIN:|42-getter|1|42-postInit
            envelopeDate.setDate(new java.util.Date(System.currentTimeMillis()));//GEN-END:|42-getter|1|42-postInit
            // write post-init user code here
        }//GEN-BEGIN:|42-getter|2|
        return envelopeDate;
    }
    //</editor-fold>//GEN-END:|42-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdProcessLogin ">//GEN-BEGIN:|51-getter|0|51-preInit
    /**
     * Returns an initiliazed instance of cmdProcessLogin component.
     * @return the initialized component instance
     */
    public Command getCmdProcessLogin() {
        if (cmdProcessLogin == null) {//GEN-END:|51-getter|0|51-preInit
            // write pre-init user code here
            cmdProcessLogin = new Command("\u0412\u043E\u0439\u0442\u0438", Command.OK, 0);//GEN-LINE:|51-getter|1|51-postInit
            // write post-init user code here
        }//GEN-BEGIN:|51-getter|2|
        return cmdProcessLogin;
    }
    //</editor-fold>//GEN-END:|51-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: accountName ">//GEN-BEGIN:|55-getter|0|55-preInit
    /**
     * Returns an initiliazed instance of accountName component.
     * @return the initialized component instance
     */
    public TextField getAccountName() {
        if (accountName == null) {//GEN-END:|55-getter|0|55-preInit
            // write pre-init user code here
            accountName = new TextField("\u041B\u043E\u0433\u0438\u043D", null, 32, TextField.ANY);//GEN-LINE:|55-getter|1|55-postInit
            // write post-init user code here
        }//GEN-BEGIN:|55-getter|2|
        return accountName;
    }
    //</editor-fold>//GEN-END:|55-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: accountPassword ">//GEN-BEGIN:|56-getter|0|56-preInit
    /**
     * Returns an initiliazed instance of accountPassword component.
     * @return the initialized component instance
     */
    public TextField getAccountPassword() {
        if (accountPassword == null) {//GEN-END:|56-getter|0|56-preInit
            // write pre-init user code here
            accountPassword = new TextField("\u041F\u0430\u0440\u043E\u043B\u044C", null, 32, TextField.ANY | TextField.PASSWORD);//GEN-LINE:|56-getter|1|56-postInit
            // write post-init user code here
        }//GEN-BEGIN:|56-getter|2|
        return accountPassword;
    }
    //</editor-fold>//GEN-END:|56-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: isAccountInfoAvailable ">//GEN-BEGIN:|60-if|0|60-preIf
    /**
     * Performs an action assigned to the isAccountInfoAvailable if-point.
     */
    public void isAccountInfoAvailable() {//GEN-END:|60-if|0|60-preIf
        // enter pre-if user code here

        if (isAccountInfoAvailable2()) {//GEN-LINE:|60-if|1|61-preAction
            switchDisplayable(null, getLoginAlert());
            
            processGrabEnvelope();
//GEN-LINE:|60-if|2|61-postAction
            // write post-action user code here
        } else {//GEN-LINE:|60-if|3|62-preAction
            // write pre-action user code here
            switchDisplayable(null, getEditAccountInfo());//GEN-LINE:|60-if|4|62-postAction
            // write post-action user code here
        }//GEN-LINE:|60-if|5|60-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|60-if|6|
    //</editor-fold>//GEN-END:|60-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSave ">//GEN-BEGIN:|70-getter|0|70-preInit
    /**
     * Returns an initiliazed instance of cmdSave component.
     * @return the initialized component instance
     */
    public Command getCmdSave() {
        if (cmdSave == null) {//GEN-END:|70-getter|0|70-preInit
            // write pre-init user code here
            cmdSave = new Command("\u0421\u043E\u0445\u0440.", Command.OK, 0);//GEN-LINE:|70-getter|1|70-postInit
            // write post-init user code here
        }//GEN-BEGIN:|70-getter|2|
        return cmdSave;
    }
    //</editor-fold>//GEN-END:|70-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: personChoise ">//GEN-BEGIN:|67-getter|0|67-preInit
    /**
     * Returns an initiliazed instance of personChoise component.
     * @return the initialized component instance
     */
    public ChoiceGroup getPersonChoise() {
        if (personChoise == null) {//GEN-END:|67-getter|0|67-preInit
            // write pre-init user code here
            personChoise = new ChoiceGroup("\u041A\u0430\u043A \u0432\u0430\u0441 \u0437\u043E\u0432\u0443\u0442?", Choice.EXCLUSIVE);//GEN-BEGIN:|67-getter|1|67-postInit
            personChoise.setSelectedFlags(new boolean[] {  });//GEN-END:|67-getter|1|67-postInit
            // write post-init user code here
        }//GEN-BEGIN:|67-getter|2|
        return personChoise;
    }
    //</editor-fold>//GEN-END:|67-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdCancel ">//GEN-BEGIN:|79-getter|0|79-preInit
    /**
     * Returns an initiliazed instance of cmdCancel component.
     * @return the initialized component instance
     */
    public Command getCmdCancel() {
        if (cmdCancel == null) {//GEN-END:|79-getter|0|79-preInit
            // write pre-init user code here
            cmdCancel = new Command("\u041E\u0442\u043C\u0435\u043D\u0430", Command.BACK, 1);//GEN-LINE:|79-getter|1|79-postInit
            // write post-init user code here
        }//GEN-BEGIN:|79-getter|2|
        return cmdCancel;
    }
    //</editor-fold>//GEN-END:|79-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdExit ">//GEN-BEGIN:|87-getter|0|87-preInit
    /**
     * Returns an initiliazed instance of cmdExit component.
     * @return the initialized component instance
     */
    public Command getCmdExit() {
        if (cmdExit == null) {//GEN-END:|87-getter|0|87-preInit
            // write pre-init user code here
            cmdExit = new Command("\u0412\u044B\u0445\u043E\u0434", Command.BACK, 2);//GEN-LINE:|87-getter|1|87-postInit
            // write post-init user code here
        }//GEN-BEGIN:|87-getter|2|
        return cmdExit;
    }
    //</editor-fold>//GEN-END:|87-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: status ">//GEN-BEGIN:|141-getter|0|141-preInit
    /**
     * Returns an initiliazed instance of status component.
     * @return the initialized component instance
     */
    public StringItem getStatus() {
        if (status == null) {//GEN-END:|141-getter|0|141-preInit
            // write pre-init user code here
            status = new StringItem("\u0421\u0442\u0430\u0442\u0443\u0441 \u043E\u043F\u0435\u0440\u0430\u0446\u0438\u0438:", null);//GEN-LINE:|141-getter|1|141-postInit
            // write post-init user code here
        }//GEN-BEGIN:|141-getter|2|
        return status;
    }
    //</editor-fold>//GEN-END:|141-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: connectionStatus ">//GEN-BEGIN:|148-getter|0|148-preInit
    /**
     * Returns an initiliazed instance of connectionStatus component.
     * @return the initialized component instance
     */
    public StringItem getConnectionStatus() {
        if (connectionStatus == null) {//GEN-END:|148-getter|0|148-preInit
            // write pre-init user code here
            connectionStatus = new StringItem("\u0421\u043E\u0441\u0442\u043E\u044F\u043D\u0438\u0435 \u043F\u043E\u0434\u043A\u043B\u044E\u0447\u0435\u043D\u0438\u044F:", "\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u043D\u0430\u0441\u0442\u0440\u043E\u0439\u043A\u0438 \u0430\u043A\u043A\u0430\u0443\u043D\u0442\u0430");//GEN-LINE:|148-getter|1|148-postInit
            // write post-init user code here
        }//GEN-BEGIN:|148-getter|2|
        return connectionStatus;
    }
    //</editor-fold>//GEN-END:|148-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: about ">//GEN-BEGIN:|157-getter|0|157-preInit
    /**
     * Returns an initiliazed instance of about component.
     * @return the initialized component instance
     */
    public Form getAbout() {
        if (about == null) {//GEN-END:|157-getter|0|157-preInit
            // write pre-init user code here
            about = new Form("\u041E \u043F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u0435", new Item[] { getLogoAbout(), getAboutText() });//GEN-BEGIN:|157-getter|1|157-postInit
            about.addCommand(getCmdBack());
            about.setCommandListener(this);//GEN-END:|157-getter|1|157-postInit
            // write post-init user code here
        }//GEN-BEGIN:|157-getter|2|
        return about;
    }
    //</editor-fold>//GEN-END:|157-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: aboutText ">//GEN-BEGIN:|158-getter|0|158-preInit
    /**
     * Returns an initiliazed instance of aboutText component.
     * @return the initialized component instance
     */
    public StringItem getAboutText() {
        if (aboutText == null) {//GEN-END:|158-getter|0|158-preInit
            // write pre-init user code here
            aboutText = new StringItem("", "\u041C\u043E\u0431\u0438\u043B\u044C\u043D\u044B\u0439 \u043A\u043B\u0438\u0435\u043D\u0442 \u0434\u043B\u044F \u0437\u0430\u043D\u0435\u0441\u0435\u043D\u0438\u044F \u0437\u0430\u0442\u0440\u0430\u0442 \u0432 \u0441\u0438\u0441\u0442\u0435\u043C\u0443 www.4konverta.com\n\n\u0421\u0430\u0439\u0442 \u043F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u044B: http://code.google.com/p/4konverta-mobile\n\u0410\u0432\u0442\u043E\u0440: \u0414\u043C\u0438\u0442\u0440\u0438\u0439 \u0413\u0443\u0441\u0435\u0432 (http://dmitrygusev.blogspot.com)\n\n\u042D\u0442\u0430 \u043F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u0430 \u2015 \u0441\u0432\u043E\u0431\u043E\u0434\u043D\u043E\u0435 \u043F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u043D\u043E\u0435 \u043E\u0431\u0435\u0441\u043F\u0435\u0447\u0435\u043D\u0438\u0435. \u0412\u044B \u043C\u043E\u0436\u0435\u0442\u0435 \u0440\u0430\u0441\u043F\u0440\u043E\u0441\u0442\u0440\u0430\u043D\u044F\u0442\u044C \u0438\u043B\u0438\n\u0438\u0437\u043C\u0435\u043D\u044F\u0442\u044C \u0435\u0433\u043E \u043F\u0440\u0438 \u0443\u0441\u043B\u043E\u0432\u0438\u044F\u0445 \u0441\u043E\u043E\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044F GNU General Public License\n\u043E\u043F\u0443\u0431\u043B\u0438\u043A\u043E\u0432\u0430\u043D\u043D\u043E\u0439 Free Software Foundation; \u043B\u0438\u0431\u043E \u0432\u0435\u0440\u0441\u0438\u0438 2 \u043B\u0438\u0446\u0435\u043D\u0437\u0438\u0438, \u043B\u0438\u0431\u043E (\u043D\u0430\n\u0432\u0430\u0448\u0435 \u0443\u0441\u043C\u043E\u0442\u0440\u0435\u043D\u0438\u0435) \u043B\u044E\u0431\u043E\u0439 \u0431\u043E\u043B\u0435\u0435 \u043F\u043E\u0437\u0434\u043D\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438.\n\n\u041F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u0430 \u0440\u0430\u0441\u043F\u0440\u043E\u0441\u0442\u0440\u0430\u043D\u044F\u0435\u0442\u0441\u044F \u0432 \u043D\u0430\u0434\u0435\u0436\u0434\u0435 \u043D\u0430 \u0442\u043E, \u0447\u0442\u043E \u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043F\u043E\u043B\u0435\u0437\u043D\u043E, \u043D\u043E\n\u0411\u0415\u0417 \u0412\u0421\u042F\u041A\u0418\u0425 \u0413\u0410\u0420\u0410\u041D\u0422\u0418\u0419; \u043D\u0435 \u0433\u0430\u0440\u0430\u043D\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044F \u0434\u0430\u0436\u0435 \u041F\u0420\u0418\u0413\u041E\u0414\u041D\u041E\u0421\u0422\u042C \u0438\u043B\u0438 \u0421\u041E\u041E\u0422\u0412\u0415\u0421\u0422\u0412\u0418\u0415 \u041A\u0410\u041A\u0418\u041C-\n\u041B\u0418\u0411\u041E \u0422\u0420\u0415\u0411\u041E\u0412\u0410\u041D\u0418\u042F\u041C. \u0414\u043B\u044F \u043F\u043E\u043B\u0443\u0447\u0435\u043D\u0438\u044F \u0434\u043E\u043F\u043E\u043B\u043D\u0438\u0442\u0435\u043B\u044C\u043D\u043E\u0439 \u0438\u043D\u0444\u043E\u0440\u043C\u0430\u0446\u0438\u0438 \u043E\u0437\u043D\u0430\u043A\u043E\u043C\u044C\u0442\u0435\u0441\u044C \u0441 GNU\nGeneral Public License.\n\n\u0412\u044B \u0434\u043E\u043B\u0436\u043D\u044B \u043F\u043E\u043B\u0443\u0447\u0438\u0442\u044C \u043A\u043E\u043F\u0438\u044E GNU General Public License \u0432\u043C\u0435\u0441\u0442\u0435 \u0441 \u043F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u043E\u0439. \u0415\u0441\u043B\u0438\n\u044D\u0442\u043E\u0433\u043E \u043D\u0435 \u043F\u0440\u043E\u0438\u0437\u043E\u0448\u043B\u043E, \u043D\u0430\u043F\u0438\u0448\u0438\u0442\u0435 to the Free Software Foundation, Inc., 59\nTemple Place, Suite 330, Boston, MA  02111-1307  USA\n");//GEN-LINE:|158-getter|1|158-postInit
            // write post-init user code here
        }//GEN-BEGIN:|158-getter|2|
        return aboutText;
    }
    //</editor-fold>//GEN-END:|158-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdAbout ">//GEN-BEGIN:|159-getter|0|159-preInit
    /**
     * Returns an initiliazed instance of cmdAbout component.
     * @return the initialized component instance
     */
    public Command getCmdAbout() {
        if (cmdAbout == null) {//GEN-END:|159-getter|0|159-preInit
            // write pre-init user code here
            cmdAbout = new Command("\u041E \u043F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u0435", Command.ITEM, 2);//GEN-LINE:|159-getter|1|159-postInit
            // write post-init user code here
        }//GEN-BEGIN:|159-getter|2|
        return cmdAbout;
    }
    //</editor-fold>//GEN-END:|159-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdBack ">//GEN-BEGIN:|168-getter|0|168-preInit
    /**
     * Returns an initiliazed instance of cmdBack component.
     * @return the initialized component instance
     */
    public Command getCmdBack() {
        if (cmdBack == null) {//GEN-END:|168-getter|0|168-preInit
            // write pre-init user code here
            cmdBack = new Command("\u041D\u0430\u0437\u0430\u0434", Command.BACK, 3);//GEN-LINE:|168-getter|1|168-postInit
            // write post-init user code here
        }//GEN-BEGIN:|168-getter|2|
        return cmdBack;
    }
    //</editor-fold>//GEN-END:|168-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdExitTypeExit ">//GEN-BEGIN:|176-getter|0|176-preInit
    /**
     * Returns an initiliazed instance of cmdExitTypeExit component.
     * @return the initialized component instance
     */
    public Command getCmdExitTypeExit() {
        if (cmdExitTypeExit == null) {//GEN-END:|176-getter|0|176-preInit
            // write pre-init user code here
            cmdExitTypeExit = new Command("\u0412\u044B\u0445\u043E\u0434", Command.EXIT, 1);//GEN-LINE:|176-getter|1|176-postInit
            // write post-init user code here
        }//GEN-BEGIN:|176-getter|2|
        return cmdExitTypeExit;
    }
    //</editor-fold>//GEN-END:|176-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdShowEnvelopeDetails ">//GEN-BEGIN:|181-getter|0|181-preInit
    /**
     * Returns an initiliazed instance of cmdShowEnvelopeDetails component.
     * @return the initialized component instance
     */
    public Command getCmdShowEnvelopeDetails() {
        if (cmdShowEnvelopeDetails == null) {//GEN-END:|181-getter|0|181-preInit
            // write pre-init user code here
            cmdShowEnvelopeDetails = new Command("\u0427\u0442\u043E \u0432 \u043A\u043E\u043D\u0432\u0435\u0440\u0442\u0435?..", Command.ITEM, 0);//GEN-LINE:|181-getter|1|181-postInit
            // write post-init user code here
        }//GEN-BEGIN:|181-getter|2|
        return cmdShowEnvelopeDetails;
    }
    //</editor-fold>//GEN-END:|181-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdRefreshEnvelope ">//GEN-BEGIN:|183-getter|0|183-preInit
    /**
     * Returns an initiliazed instance of cmdRefreshEnvelope component.
     * @return the initialized component instance
     */
    public Command getCmdRefreshEnvelope() {
        if (cmdRefreshEnvelope == null) {//GEN-END:|183-getter|0|183-preInit
            // write pre-init user code here
            cmdRefreshEnvelope = new Command("\u041E\u0431\u043D\u043E\u0432\u0438\u0442\u044C", Command.ITEM, 2);//GEN-LINE:|183-getter|1|183-postInit
            // write post-init user code here
        }//GEN-BEGIN:|183-getter|2|
        return cmdRefreshEnvelope;
    }
    //</editor-fold>//GEN-END:|183-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: envelopeDetails ">//GEN-BEGIN:|180-getter|0|180-preInit
    /**
     * Returns an initiliazed instance of envelopeDetails component.
     * @return the initialized component instance
     */
    public Form getEnvelopeDetails() {
        if (envelopeDetails == null) {//GEN-END:|180-getter|0|180-preInit
            // write pre-init user code here
            envelopeDetails = new Form("\u041A\u043E\u043D\u0432\u0435\u0440\u0442 2008-7-18", new Item[] { getDailyBudget(), getSpentLeftForADay(), getWeeklyBudget(), getSpentLeftForTheWeek() });//GEN-BEGIN:|180-getter|1|180-postInit
            envelopeDetails.addCommand(getCmdRefreshEnvelope());
            envelopeDetails.addCommand(getCmdBack());
            envelopeDetails.setCommandListener(this);//GEN-END:|180-getter|1|180-postInit
            // write post-init user code here
        }//GEN-BEGIN:|180-getter|2|
        return envelopeDetails;
    }
    //</editor-fold>//GEN-END:|180-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dailyBudget ">//GEN-BEGIN:|193-getter|0|193-preInit
    /**
     * Returns an initiliazed instance of dailyBudget component.
     * @return the initialized component instance
     */
    public StringItem getDailyBudget() {
        if (dailyBudget == null) {//GEN-END:|193-getter|0|193-preInit
            // write pre-init user code here
            dailyBudget = new StringItem("\u0411\u044E\u0434\u0436\u0435\u0442 \u043D\u0430 \u0434\u0435\u043D\u044C:", null);//GEN-LINE:|193-getter|1|193-postInit
            // write post-init user code here
        }//GEN-BEGIN:|193-getter|2|
        return dailyBudget;
    }
    //</editor-fold>//GEN-END:|193-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spentLeftForADay ">//GEN-BEGIN:|194-getter|0|194-preInit
    /**
     * Returns an initiliazed instance of spentLeftForADay component.
     * @return the initialized component instance
     */
    public StringItem getSpentLeftForADay() {
        if (spentLeftForADay == null) {//GEN-END:|194-getter|0|194-preInit
            // write pre-init user code here
            spentLeftForADay = new StringItem("\u041F\u043E\u0442\u0440\u0430\u0447\u0435\u043D\u043E \u0437\u0430 \u0434\u0435\u043D\u044C/\u043E\u0441\u0442\u0430\u043B\u043E\u0441\u044C:", null);//GEN-LINE:|194-getter|1|194-postInit
            // write post-init user code here
        }//GEN-BEGIN:|194-getter|2|
        return spentLeftForADay;
    }
    //</editor-fold>//GEN-END:|194-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spentLeftForTheWeek ">//GEN-BEGIN:|196-getter|0|196-preInit
    /**
     * Returns an initiliazed instance of spentLeftForTheWeek component.
     * @return the initialized component instance
     */
    public StringItem getSpentLeftForTheWeek() {
        if (spentLeftForTheWeek == null) {//GEN-END:|196-getter|0|196-preInit
            // write pre-init user code here
            spentLeftForTheWeek = new StringItem("\u041F\u043E\u0442\u0440\u0430\u0447\u0435\u043D\u043E \u0437\u0430 \u043D\u0435\u0434\u0435\u043B\u044E/\u043E\u0441\u0442\u0430\u043B\u043E\u0441\u044C:", null);//GEN-LINE:|196-getter|1|196-postInit
            // write post-init user code here
        }//GEN-BEGIN:|196-getter|2|
        return spentLeftForTheWeek;
    }
    //</editor-fold>//GEN-END:|196-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: weeklyBudget ">//GEN-BEGIN:|197-getter|0|197-preInit
    /**
     * Returns an initiliazed instance of weeklyBudget component.
     * @return the initialized component instance
     */
    public StringItem getWeeklyBudget() {
        if (weeklyBudget == null) {//GEN-END:|197-getter|0|197-preInit
            // write pre-init user code here
            weeklyBudget = new StringItem("\u0411\u044E\u0434\u0436\u0435\u0442 \u043D\u0430 \u043D\u0435\u0434\u0435\u043B\u044E:", null);//GEN-LINE:|197-getter|1|197-postInit
            // write post-init user code here
        }//GEN-BEGIN:|197-getter|2|
        return weeklyBudget;
    }
    //</editor-fold>//GEN-END:|197-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|204-getter|0|204-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|204-getter|0|204-preInit
            // write pre-init user code here
            okCommand = new Command("\u041E\u041A", Command.OK, 0);//GEN-LINE:|204-getter|1|204-postInit
            // write post-init user code here
        }//GEN-BEGIN:|204-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|204-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loginAlert ">//GEN-BEGIN:|203-getter|0|203-preInit
    /**
     * Returns an initiliazed instance of loginAlert component.
     * @return the initialized component instance
     */
    public Alert getLoginAlert() {
        if (loginAlert == null) {//GEN-END:|203-getter|0|203-preInit
            // write pre-init user code here
            loginAlert = new Alert("4 \u041A\u043E\u043D\u0432\u0435\u0440\u0442\u0430", "\u0412\u044B\u043F\u043E\u043B\u043D\u044F\u0435\u0442\u0441\u044F \u0432\u0445\u043E\u0434...", getLogo(), null);//GEN-BEGIN:|203-getter|1|203-postInit
            loginAlert.addCommand(getOkCommand());
            loginAlert.setCommandListener(this);
            loginAlert.setTimeout(Alert.FOREVER);//GEN-END:|203-getter|1|203-postInit
            // write post-init user code here
        }//GEN-BEGIN:|203-getter|2|
        return loginAlert;
    }
    //</editor-fold>//GEN-END:|203-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: logo ">//GEN-BEGIN:|207-getter|0|207-preInit
    /**
     * Returns an initiliazed instance of logo component.
     * @return the initialized component instance
     */
    public Image getLogo() {
        if (logo == null) {//GEN-END:|207-getter|0|207-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|207-getter|1|207-@java.io.IOException
                logo = Image.createImage("/favicon64x64.png");
            } catch (java.io.IOException e) {//GEN-END:|207-getter|1|207-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|207-getter|2|207-postInit
            // write post-init user code here
        }//GEN-BEGIN:|207-getter|3|
        return logo;
    }
    //</editor-fold>//GEN-END:|207-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: logoAbout ">//GEN-BEGIN:|213-getter|0|213-preInit
    /**
     * Returns an initiliazed instance of logoAbout component.
     * @return the initialized component instance
     */
    public ImageItem getLogoAbout() {
        if (logoAbout == null) {//GEN-END:|213-getter|0|213-preInit
            // write pre-init user code here
            logoAbout = new ImageItem("", getLogo(), ImageItem.LAYOUT_DEFAULT, "<Missing Image>");//GEN-LINE:|213-getter|1|213-postInit
            // write post-init user code here
        }//GEN-BEGIN:|213-getter|2|
        return logoAbout;
    }
    //</editor-fold>//GEN-END:|213-getter|2|

    private void prepareEditExpenses() throws LoginException, Exception {
        final Date newDate = getEnvelopeDate().getDate();
        
        if (! as.getEnvelopeController().includes(newDate)) {
            grabEnvelope(newDate);
        }
         
        Person person = getSelectedPerson();

        try {
            as.getAppConfig().updatePersonId(person.getId());
        } catch (RecordStoreException e) {
            getStatus().setText("Ошибка 4k-m-502: " + e.getClass().getName() + ": " + e.getMessage());
        }

        int dow = getSelectedDayOfWeek();

        String expenses = person.getEnvelope().getExpensesForDay(dow);

        getEditExpenses().setString(expenses);

        getEditExpenses().setTitle(
                person.getName() + " - " + Utils.formatDate(newDate));
    }
    
    private void processGrabEnvelope() {
        try {
            final Date today = getEnvelopeDate().getDate();
            
            grabEnvelope(today);
            
            switchDisplayable(null, getSelectEnvelopeDate());
        }
        catch (LoginException e) {
            getConnectionStatus().setText(e.getMessage());
            
            as.getAppConfig().resetAccountPassword();
            getAccountPassword().setString("");
            
            switchDisplayable(null, getEditAccountInfo());
        }
        catch (SecurityException e) {
            String message = Utils.isNullOrEmpty(e.getMessage()) 
                    ? "Приложению запрещен доступ в Интернет" 
                    : e.getMessage();
            
            getConnectionStatus().setText("Ошибка 4k-m-509: " + e.getClass().getName() + ": " + message);
            getStatus().setText("Ошибка 4k-m-509: " + e.getClass().getName() + ": " + message);
            
            switchDisplayable(null, getEditAccountInfo());
        }
        catch (IOException e) {
            String message = Utils.isNullOrEmpty(e.getMessage()) 
                    ? "Не могу соединиться с сервисом www.4konverta.com" 
                    : e.getMessage();
            
            getConnectionStatus().setText("Ошибка 4k-m-503: " + e.getClass().getName() + ": " + message);
            getStatus().setText("Ошибка 4k-m-503: " + e.getClass().getName() + ": " + message);
            
            switchDisplayable(null, getEditAccountInfo());
        }
        catch (Exception e) {
            getConnectionStatus().setText("Ошибка 4k-m-521: " + e.getClass().getName() + ": " + e.getMessage());
            getStatus().setText("Ошибка 4k-m-521: " + e.getClass().getName() + ": " + e.getMessage());
            
            switchDisplayable(null, getEditAccountInfo());
        }
    }
    
    private void processSaveExpenses() throws IOException {
        int dow = getSelectedDayOfWeek();

        String expenses = getEditExpenses().getString();

        Person person = getSelectedPerson();

        try {
            as.getEnvelopeController().saveExpenses(
                    dow, person.getId(), expenses);

            person.getEnvelope().setExpensesForDay(dow, expenses);
            
            getStatus().setText("Затраты записаны");
        } catch (Exception e) {
            getStatus().setText("Ошибка 4k-m-504: " + e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private void grabEnvelope(Date date) throws LoginException, Exception {
        getStatus().setText(
                "Загружаю конверт на " + Utils.formatDate(date) + "...");
        
        authenticateUsingCookies();

        getLoginAlert().setString("Загрузка конверта...");
        
        EnvelopeController ec = c.grabEnvelope(date);

        as.setEnvelopeController(ec);

        Vector persons = ec.getPersons();

        updatePersons(persons);

        getConnectionStatus().setText("Вход выполнен");
        getStatus().setText("Конверт загружен");
    }

    private void authenticateUsingCookies() throws LoginException, Exception {
        if (as.getCookie() == null) {
            getConnectionStatus().setText("Выполняется вход...");

            String cookie = c.obtainCookie();

            as.setCookie(cookie);

            Application app = new Application();
            app.setAppConfig(ApplicationState.getInstance().getAppConfig());
            app.setVersion(getAppProperty("MIDlet-Version"));

            new PingAction(app).execute();

            VersionChecker vc = new VersionChecker(
                    "http://code.google.com/p/4konverta-mobile/wiki/LatestBasicVersionInfo",
                    app.getVersion());

            final String latestVersionString = vc.getLatestVersionString();

            if (! latestVersionString.equals(app.getVersion())) {
                            getSelectEnvelopeDate().setTicker(
                    new Ticker("Вышла новая версия программы "
                        + latestVersionString
                        + "; подробности на сайте проекта: http://code.google.com/p/4konverta-mobile"));
            }
        }
    }

    private void updateEnvelopeDetails() {
        final EnvelopeController ec = as.getEnvelopeController();

        getDailyBudget().setText("\n" + MicroDouble.round(ec.getDailyBudget().doubleValue()));
        getWeeklyBudget().setText("\n" + MicroDouble.round(ec.getWeeklyBudget().doubleValue()));

        int dow = getSelectedDayOfWeek();

        getSpentLeftForADay().setText("\n" +
                MicroDouble.round(ec.getSpentForADayTotal(dow).doubleValue()) + " / " + 
                MicroDouble.round(ec.getLeftForADayTotal(dow).doubleValue()));

        getSpentLeftForTheWeek().setText("\n" +
                MicroDouble.round(ec.getSpentForTheWeek().doubleValue()) + " / " +
                MicroDouble.round(ec.getLeftForTheWeek().doubleValue()));

        getEnvelopeDetails().setTitle("Конверт " + Utils.formatDate(getEnvelopeDate().getDate()));
    }
    
    private void updatePersons(Vector persons) {
        while (getPersonChoise().size() > 0) {
            getPersonChoise().delete(0);
        }

        for (int i = 0; i < persons.size(); i++) {
            Person person = (Person) persons.elementAt(i);

            int idx = getPersonChoise().append(
                    person.getName(), null);

            if (as.getAppConfig().getPersonId() != null 
                    && person.getId().equals(as.getAppConfig().getPersonId()))
            {
                getPersonChoise().setSelectedIndex(idx, true);
            }
        }
    }
    
    private boolean isAccountInfoAvailable2() {
        if (! as.getAppConfig().isAccountInfoAvailable()) {
            
            String login = getAccountName().getString();
            String password = getAccountPassword().getString();

            if (! Utils.areBothSpecified(login, password)) {
                return false;
            }
            else {
                try {
                    as.getAppConfig().updateAccountInfo(login, password);
                } catch (RecordStoreException e) {
                    getStatus().setText("Ошибка 4k-m-005: " + e.getClass().getName() + ": " + e.getMessage());
                }
            }
        }

        return true;
    }

    private int getSelectedDayOfWeek() {
        Calendar cal = Calendar.getInstance();

        cal.setTime(getEnvelopeDate().getDate());

        return Utils.convertJavaDOWToEnvelopeDOW(cal.get(Calendar.DAY_OF_WEEK));
    }
    
    private Person getSelectedPerson() {
        int idx = getPersonChoise().getSelectedIndex();

        return (Person) as.getEnvelopeController().getPersons().elementAt(idx);
    }

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
    
}
